<?php

class ShareController extends Zend_Controller_Action
{
    public function init()
    {
		$this->view->baseUrl = $_SERVER['HTTP_HOST'];
    }

    public function indexAction()
    {
      $this->_helper->layout->disableLayout();
	  $this->view->followed = false;
      $req = $this->getRequest();
      $this->view->mobile = '';
      $this->view->amount = '';
      $this->view->result = '';
      $this->view->orderId = $req->get('orderId');
	  $this->view->mobile = $req->get('mobile');
	  $openId = $this->getOpenId('/share',$this->view->orderId,$this->view->mobile);
      $rest = new Application_Model_Rest();
	  
	  if($req->isGet()){//是否关注
		$GetSignPackage = new Application_Model_GetSignPackage();
		$this->view->orderDetails = $rest->getUserOrderDetails($this->view->orderId);
		$res_gz = $GetSignPackage->GetUserGz($openId);
		if($res_gz['subscribe']==0){
			$this->view->followed = true;
			//echo '<script type="text/javascript">alert("亲，先微信关注公众号鲜店才能领取现金券哦！");</script>';
		}
	  }
	  
	  if ($req->isPost()) {
        $this->view->mobile = $req->getPost('mobile');
        $this->view->orderId = $req->getPost('orderId');
		$rest = new Application_Model_Rest();
		$this->view->orderDetails = '';
		if($this->view->orderId){
			$orderDetails = $rest->getUserOrderDetails($this->view->orderId);
		}
		if($orderDetails){
			$this->view->orderDetails = $orderDetails;
		}
        if(!preg_match("/1[34578]{1}\d{9}$/",$this->view->mobile)) {
            $this->view->result = 'wrong number';
        }
      }
	  
      $this->view->VoucherList = $rest->getVoucherDetailList($this->view->orderId);
      foreach($this->view->VoucherList as $Voucher){
        if($Voucher->openid == $openId){
          $this->view->openId = $openId;
          $this->view->result = 'success';
          $this->view->discount = $Voucher->discount;
	    }
	  }
    }
	
    public function resultAction()
    {
	    $this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		$req = $this->getRequest();
		if($req->isPost()){
			$result = array('msg'=>'fail');
			$mobile = $req->getPost('mobile');
			$orderId = $req->getPost('orderId');
			$result['mobile'] = $mobile;
			$result['orderId'] = $orderId;
			$openId = $this->getOpenId('/share/result',$orderId,$mobile);
			if (empty($openId)) {
				$this->_redirect('/voucher');
			}
			$GetSignPackage = new Application_Model_GetSignPackage();
			$UserInfo = $GetSignPackage->GetUserInfo($openId);
			if (!empty($UserInfo)) {
				$nickname = $UserInfo['nickname'];
				$headimgurl = $UserInfo['headimgurl'];
				$rest = new Application_Model_Rest();
				$res = $rest->postForwardingVoucher($mobile,$orderId,$openId,$nickname,$headimgurl);
			
				if ($res) {
					$result['msg'] = 'success';
				}
			}
			echo json_encode($result);
			exit();
		}
		
    }

	private function getOpenId($uri, $orderId,$mobile) {
		$openIdNamespace = new Zend_Session_Namespace('share');
		$openId = $openIdNamespace->openId;
		if (empty($openId)) {
		  if(!array_key_exists('code',$_GET)){ //获取code
            include_once(APPLICATION_PATH."/../library/WxPayPubHelper/WxPayPubHelper.php");
            $jsApi = new JsApi_pub();
            $url = $jsApi->createOauthUrlForCode_test(urlencode('http://shop.renrencaichang.com'.$uri.'?orderId='.$orderId.'&mobile='.$mobile));
            Header("Location: $url");
            exit(0);
          }
          $code = $_GET['code'];
          $GetSignPackage = new Application_Model_GetSignPackage();
          $openId = $GetSignPackage->getOpenId($code);
          $openIdNamespace->openId= $openId;
		}
        return $openId;
	}
}
